﻿Imports System.Drawing

Public Class AquariumDetail
    Inherits ItemDetailPage

    Protected Property PhotoNormalSize() As New Size() With {.width = 512, .height = 256}
    Protected Property PhotoThumbSize() As New Size() With {.width = 128, .height = 64}
    Protected Property AquariumID() As ULong

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AquariumID = CULng(Request.QueryString.Get("ID"))
        If Not IsPostBack Then
            If Not String.IsNullOrWhiteSpace(AquariumID) AndAlso AquariumExists(AquariumID) Then
                Select Case Request.QueryString.Get("Action")
                    Case "Edit"
                        DetailFormView.DefaultMode = FormViewMode.Edit
                    Case "Add"
                        DetailFormView.DefaultMode = FormViewMode.Insert
                End Select
                UpdateDataSource()
            Else
                Response.Redirect("~/Pages/AquariumList.aspx")
            End If
        End If
    End Sub

    Protected Sub SaveButton_Click(sender As Object, e As EventArgs)
        ' TODO save
        StopEditing()
    End Sub

    Protected Sub CancelButton_Click(sender As Object, e As EventArgs)
        StopEditing()
    End Sub

    Protected Sub EditButton_Click(sender As Object, e As EventArgs)
        StartEditing()
    End Sub

    Protected Function AquariumExists(ByVal ID As ULong) As Boolean
        Dim gateway As New AquariumGateway()
        Return gateway.Exists(ID)
    End Function

    Protected Sub StartEditing()
        Dim redirectUrl As String = String.Format("~/Pages/AquariumDetail.aspx?ID={0}&Action=Edit", AquariumID)
        Response.Redirect(redirectUrl)
    End Sub

    Protected Sub StopEditing()
        Dim redirectUrl As String = String.Format("~/Pages/AquariumDetail.aspx?ID={0}", AquariumID)
        Response.Redirect(redirectUrl)
    End Sub

    Protected Function getPhotoPath(ByVal fileName As String) As String
        Return PhotoUtils.getNormalPath(fileName)
    End Function

    Protected Sub UpdateDataSource()
        detailViewDataBind()
        commentViewDataBind()
    End Sub

    Private Sub detailViewDataBind()
        Dim gateway As New AquariumGateway()
        DetailFormView.DataSource = gateway.FindByID(AquariumID)
        DetailFormView.DataBind()
    End Sub

    Private Sub commentViewDataBind()
        Dim gateway As New CommentGateway()
        commentView.DataSource = gateway.FindByID(AquariumID)
        commentView.DataBind()
    End Sub
End Class